﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DietTherapy.CodeGenerator;
using SqlSugar;

namespace DietTherapy.Generator
{
    public class GEdb
    {
        public byte[] CodeGenByAll(string fileType)
        {
            SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Password=UISDFHkii_%$#sd][kodf@;Persist Security Info=True;User ID=sa;Initial Catalog=DietTherapy;Data Source=localhost;Timeout=999;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            },
            db => {

                db.Aop.OnLogExecuting = (sql, pars) =>
                {

                    //获取原生SQL推荐 5.1.4.63  性能OK
                    Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));

                    //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                    //Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))


                };

                //注意多租户 有几个设置几个
                //db.GetConnection(i).Aop

            });

           

            var tables = Db.DbMaintenance.GetTableInfoList();
            var views = Db.DbMaintenance.GetViewInfoList(false);
            var newList = tables;
            if (views.Any()) newList = tables.Union(views).ToList();

            var allDb = new List<DbTableInfoAndColumns>();
            newList.ForEach(p =>
            {
                var model = new DbTableInfoAndColumns();
                model.Name = p.Name;
                model.DbObjectType = p.DbObjectType;
                model.Description = p.Description;
                model.columns = Db.DbMaintenance.GetColumnInfosByTableName(p.Name, false);
                allDb.Add(model);
            });
            Db.Close();
            if (!allDb.Any())
                return null;
            return GeneratorCodeHelper.CodeGeneratorAll(allDb, fileType);
        }

        /// <summary>
        ///     自动生成代码
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fileType"></param>
        /// <returns></returns>
        public byte[] CodeGen(string tableName, string fileType)
        {
            SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Password=UISDFHkii_%$#sd][kodf@;Persist Security Info=True;User ID=sa;Initial Catalog=DietTherapy;Data Source=localhost;Timeout=999;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            },
          db => {

              db.Aop.OnLogExecuting = (sql, pars) =>
              {

                  //获取原生SQL推荐 5.1.4.63  性能OK
                  Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));

                  //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                  //Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))


              };

              //注意多租户 有几个设置几个
              //db.GetConnection(i).Aop

          });



            var tables = Db.DbMaintenance.GetTableInfoList(false);
            var views = Db.DbMaintenance.GetViewInfoList(false);
            var tb = tables.Find(p => p.Name == tableName) ?? views.Find(p => p.Name == tableName);
            if (tb == null)
            {
                return null;
            }

            var columns = Db.DbMaintenance.GetColumnInfosByTableName(tb.Name, false);
            Db.Close();
            return GeneratorCodeHelper.CodeGenerator(tb.Name, tb.Description, columns, fileType);
        }
    }
}
